﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace DataAccess
{
    public class DataAccessFactory
    {
        private DataAccessFactory()
        {

        }
        /// <summary>
        /// 默认的数据库连接字符串在配置文件的名称
        /// </summary>
        internal static readonly string DEFALUT_DATABASE_NAME = "DefaultConnection";

        private static DataAccessFactory dataAccessFactory = new DataAccessFactory();
        public static DataAccessFactory instance
        {
            get
            {
                return dataAccessFactory;
            }
        }

        public IDataAccess CreateDataAccess()
        {


            PersistenceProperty.ConnectionString = ConfigurationManager.ConnectionStrings[DEFALUT_DATABASE_NAME].ConnectionString;
            //PersistenceProperty.ConnectionString = "Data Source=HISSVR;Initial Catalog=THIS4;Persist Security Info=True;User ID=sa;Password=sasa;";
           // PersistenceProperty.ConnectionString = "Data Source=.;Initial Catalog=THIS4;Persist Security Info=True;User ID=sa;Password=;";

            PersistenceProperty.DatabaseType = DatabaseType.MSSQLServer;

            IDataAccess dataAccess = null;
            switch (PersistenceProperty.DatabaseType)
            {
                case (DatabaseType.MSSQLServer):
                    dataAccess = new MSSqlDataAccess(PersistenceProperty.ConnectionString);
                    break;
                case (DatabaseType.Oracle):
                    dataAccess = new OracleDataAccess(PersistenceProperty.ConnectionString);
                    break;
                ////添加Oracle等数据库的判断。
                //case DatabaseType.DB2:
                //    dataAccess = new DB2DataAccess(PersistenceProperty.ConnectionString);
                //    break;
                default:
                    //添加其它等数据库的判断。
                    break;
            }
            return dataAccess;
        }
    }
}
